﻿using System;
using System.Web.UI;
using VitCode.Castle.DataAccess;
using VitCode.Castle.Helper;
using VitCode.Castle.Management;


namespace CRS.Web.BackStageManagement
{
    public partial class UserGroupInsert : VcPage
    {
        readonly BaseDataManager help = new BaseDataManager();

       protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>");
           if (!IsPostBack)
            {
                labelId.Text = publ.GetUrlToReceive(Request.QueryString["id"].ToString().Trim());
            }

        }
 
   

        protected void Button_Insert_Click1(object sender, EventArgs e)
        {
            CastleAccess acc = new CastleAccess();
            var objUserGroup = new VitCode.Castle.Entities.UserGroup();
            string strName = ZLTextBox_UserGroupName.Text.Trim();
            string strTypeName = ZLTextBox_TypeName.Text.Trim();
            string strMemo = ZLTextBox_Memo.Text.Trim();
            string strParentId = labelId.Text.Trim();
            objUserGroup.Name = strName;
            objUserGroup.TypeName = strTypeName;
            objUserGroup.Memo = strMemo;

            if (labelId.Text.Trim().Length == 36)
            {
                var lstObj = acc.GetObjectList(new UserGroup().GetType().Name, "Id", strParentId);
                if (lstObj != null && lstObj.Count > 0)
                {
                    var lstObj2 = acc.GetObjectList(new UserGroup().GetType().Name, "Name", strName);
                    if (lstObj2 != null && lstObj2.Count > 0)
                    {
                        Alert.MessageBox("名称为: " + strName + "  的商户已存在，请检测！！！");
                        return;
                    }
                    var objParentUserGroup = (VitCode.Castle.Entities.UserGroup)(lstObj[0]);
                    objUserGroup.Parent = objParentUserGroup;
                }
            }
            else
            {
                var lstObj3 = acc.GetObjectList(new UserGroup().GetType().Name, "Name", strName);
                if (lstObj3 != null && lstObj3.Count > 0)
                {
                    Alert.MessageBox("名称为: " + strName + "  的商户已存在，请检测！！！");
                    return;
                }
            }

            bool access = help.SaveOrUpdateObj(objUserGroup);
            if (access)
            {
                Alert.MessageBox("添加成功!");
            }
        }

        protected void Button_Cancel_Click(object sender, EventArgs e)
        {

            Response.Redirect("UserGroupList.aspx", false);
        }
    }
}
